import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.*;

import javax.swing.*;
import javax.swing.Timer;


//This is the panel. Put the static arrays here.

public class RoboticsProjectPanel extends JPanel{

	public final static int X_DIMENSION = 1200, Y_DIMENSION = 600;

	public static Robot[][] robotList = new Robot[2][3];

	/*For reference:
	 *			 0				  1
	 *	0	  Blue 1 (User)		Red 1
	 *	1	  Blue 2			Red 2
	 *	2	  Blue 3			Red 3
	 */

	public static Path[] pathList = new Path[6];
	public static ArrayList<GamePiece> gamePieceList = new ArrayList<GamePiece>();
	public static Field field;
	public static int bluePoints = 0, redPoints = 0;
	private HelpFrame help;
	public static Timer repainter;
	public static final ImageIcon FIRST = new ImageIcon("Logo.png");
	public static Color background = new Color(230,230,230);

	public RoboticsProjectPanel() {

		setFocusable(true);
		addKeyListener(new HelpMenuListener());

		setPreferredSize(new Dimension(X_DIMENSION, Y_DIMENSION));
		setBackground(background);
		setLayout(null);

		help = new HelpFrame();
		repainter = new Timer(10, new TimeListener());
		repainter.start();
	}

	public void paintComponent(Graphics page){
		super.paintComponent(page);

		//draws the FIRST logo
		FIRST.paintIcon(null, page, X_DIMENSION/2 - FIRST.getIconWidth()/2, Y_DIMENSION/2 - FIRST.getIconWidth()/2 + 20);

		//draws the field
		field.drawField(page);

		//background
		page.setColor(Color.BLACK);
		page.fillRect(0, 0, X_DIMENSION, 10);
		page.fillRect(0, Y_DIMENSION - 10, X_DIMENSION, Y_DIMENSION);
		page.fillRect(X_DIMENSION - 10, 0, X_DIMENSION, Y_DIMENSION);
		page.fillRect(0, 0, 10, Y_DIMENSION);


		//draws all the robots
		for(int i=0; i<3; i++){
			robotList[0][i].drawRobot(page);
			robotList[1][i].drawRobot(page);
		}

		//draws the GamePieces
		for(int i=0; i<gamePieceList.size(); i++){

			gamePieceList.get(i).drawGamePiece(page, field.getFieldType());

		}
	}

	//Returns true if all the game pieces have been collected
	public static boolean allGamePiecesCollected() {
		boolean temp = true;

		for(int i = 0; i < gamePieceList.size(); i++){
			if (gamePieceList.get(i).isCarried() == false) {
				temp = false;
			}
		}
		return temp;
	}

	//This is the call to the move method in the robot class
	public static synchronized void move(Robot robut, Point destination){

		if(robut.moveIsValid(destination)){
			robut.move(destination);
		}

		//also, update the boundaries
		field.updateBoundaries(robotList);
	}

	//Allows the user to access the help menu by pressing the h key
	private class HelpMenuListener implements KeyListener {

		public void keyPressed(KeyEvent event) {
			if (event.getKeyCode() == KeyEvent.VK_H){
				help.setVisible(true);
			}

			if (event.getKeyCode() == KeyEvent.VK_ESCAPE){
				help.setVisible(false);
			}
		}

		//Overridden methods
		public void keyReleased(KeyEvent event) {}
		public void keyTyped(KeyEvent event) {}

	}

	private class TimeListener implements ActionListener{

		public void actionPerformed(ActionEvent event) {

			repaint(); //repaint first

			//Update boundaries, wake up threads for next round of movement
			RoboticsProjectPanel.field.updateBoundaries(robotList);
			for (int i = 0; i < 2 ; i++){
				for (int j = 0; j < 3; j++){
					robotList[i][j].wakeThread();
				}
			}
		}
	}

	//creates ArrayList of robots
	public static ArrayList<Robot> getRobotArrayList(){
		ArrayList<Robot> robotArrayList = new ArrayList<Robot>();
		for(int i=0; i<3; i++){
			robotArrayList.add(robotList[0][i]);
		}
		for(int n=0; n<3; n++){
			robotArrayList.add(robotList[1][n]);
		}
		return robotArrayList;
	}
}
